package com.nwcl.ysx.controller;

import com.nwcl.basic.bo.CouponsReceiveBO;
import com.nwcl.basic.bo.CouponsWriteOffBO;
import com.nwcl.basic.service.CouponsReceiveService;
import com.nwcl.basic.service.CouponsWriteOffService;
import com.nwcl.basic.service.UserRoleService;
import com.nwcl.common.constant.Constant;
import com.nwcl.common.result.Result;
import com.nwcl.common.utils.DateUtil;
import com.nwcl.common.utils.SessionUtil;
import com.nwcl.ysx.request.CouponsWriteOffRequest;
import com.nwcl.ysx.request.CouponsReceiveRequest;
import com.nwcl.ysx.vo.CouponsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/*
 * @Description: 优惠券功能接口
 * @Author liguiqin
 * @Date 2023/5/30 9:25
 * @param: null
 * @return
 * @UpdateDate 2023/5/30 9:25
 **/
@RestController
@RequestMapping("/api/ysx/coupons")
public class ApiCouponsController {
    @Autowired
    private SessionUtil sessionUtil;
    @Autowired
    private CouponsReceiveService couponsReceiveService;
    @Autowired
    private CouponsWriteOffService couponsWriteOffService;
    @Autowired
    private UserRoleService userRoleService;

    /*
     * @Description: 优惠券列表
     * @Author liguiqin
     * @Date 2023/5/30 9:57
     * @param: request
     * @return com.nwcl.common.result.Result
     * @UpdateDate 2023/5/30 9:57
     **/
    @GetMapping("/list")
    public Result list(HttpServletRequest request) {
        Integer userId = sessionUtil.getUserId(request);
        List<CouponsReceiveBO> boList = couponsReceiveService.listByUserIdAndTime(userId, new Date());
        Map<Integer, CouponsReceiveBO> couponsReceiveMap = new HashMap<>();
        for (CouponsReceiveBO receiveBO : boList) {
            couponsReceiveMap.put(receiveBO.getCouponsId(),receiveBO);
        }
        List<CouponsVO> list = new ArrayList<>();
        Constant.Coupons.valMap.forEach((k, v) -> {
            CouponsVO vo = new CouponsVO();
            vo.setId(k);
            vo.setName(v);
            CouponsReceiveBO receiveBO = couponsReceiveMap.get(k);
            if (receiveBO != null) {
                vo.setStatus(receiveBO.getStatus());
                vo.setStatusStr(Constant.CouponsReceiveStatus.valMap.get(vo.getStatus()));
            }
            if(receiveBO==null){
                vo.setStatus(Constant.CouponsReceiveStatus.toReceive);
                vo.setStatusStr(Constant.CouponsReceiveStatus.valMap.get(vo.getStatus()));
            }
            list.add(vo);
        });
        return Result.success(list);
    }

    /*
     * @Description: 优惠券领取
     * @Author liguiqin
     * @Date 2023/5/30 9:25
     * @param: null
     * @return
     * @UpdateDate 2023/5/30 9:25
     **/
    @PostMapping("/receive")
    public Result receive(@RequestBody CouponsReceiveRequest param, HttpServletRequest request) {
        Result rs=param.check();
        if(rs!=null){
            return rs;
        }
        List<Integer> newCoupons = param.getCouponsIds();
        Integer userId = sessionUtil.getUserId(request);
        List<CouponsReceiveBO> boList = couponsReceiveService.listByUserIdAndTime(userId, new Date());
        List<Integer> couponsIds = new ArrayList<>();
        for (CouponsReceiveBO receiveBO : boList) {
            if (newCoupons.contains(receiveBO.getCouponsId())) {
                return Result.fail("优惠券已领取,请勿重复领取");
            }
            couponsIds.add(receiveBO.getCouponsId());
        }
        //领取优惠券
        for (Integer couponsId : newCoupons) {
            if (!couponsIds.contains(couponsId)) {
                CouponsReceiveBO receiveBO = new CouponsReceiveBO();
                receiveBO.setCouponsId(couponsId);
                receiveBO.setStatus(Constant.CouponsReceiveStatus.pend);
                receiveBO.setUserId(userId);
                receiveBO.setStartTime(new Date());
                receiveBO.setEndTime(DateUtil.getMaxDate());
                receiveBO.setCreateTime(new Date());
                receiveBO.setUpdateTime(new Date());
                receiveBO.setIsValid(Constant.isValid.valid);
                couponsReceiveService.save(receiveBO);
            }
        }
        return Result.success();
    }

    /*
     * @Description: 优惠券核销
     * @Author liguiqin
     * @Date 2023/5/30 10:12
     * @param: param
     * @return com.nwcl.common.result.Result
     * @UpdateDate 2023/5/30 10:12
     **/
    @PostMapping("/writeOff")
    public Result writeOff(@RequestBody CouponsWriteOffRequest param, HttpServletRequest request) {
        Result rs = param.check();
        if (rs != null) {
            return rs;
        }
        Integer userId = sessionUtil.getUserId(request);
        List<Integer> roleIds = userRoleService.listByUserId(userId);
        if (!roleIds.contains(Constant.UserRole.admin) && !roleIds.contains(Constant.UserRole.restaurant)) {
            return Result.fail("对不起,您无核销权限");
        }
        CouponsReceiveBO receiveBO = couponsReceiveService.getById(param.getCouponsReceiveId());
        if (receiveBO == null) {
            return Result.fail("优惠券无法核销");
        }
        if (!DateUtil.isBetweenTime(receiveBO.getStartTime(), receiveBO.getEndTime(), new Date())) {
            return Result.fail("优惠券已过期");
        }
        if (receiveBO.getStatus() == Constant.CouponsReceiveStatus.used) {
            return Result.fail("优惠券已使用");
        }
        if (receiveBO.getStatus() == Constant.CouponsReceiveStatus.expired) {
            return Result.fail("优惠券已过期");
        }
        CouponsWriteOffBO writeOffBO = new CouponsWriteOffBO();
        writeOffBO.setCouponsReceiveId(receiveBO.getId());
        writeOffBO.setUserId(userId);
        writeOffBO.setUseTime(new Date());
        writeOffBO.setCreateTime(new Date());
        writeOffBO.setUpdateTime(new Date());
        writeOffBO.setIsValid(Constant.isValid.valid);
        couponsWriteOffService.save(writeOffBO);
        couponsReceiveService.updateStatus(receiveBO.getId(), Constant.CouponsReceiveStatus.used);
        return Result.success();
    }
}
